package com.filter;

import com.controller.bean.Login;
import com.entites.Employee;
import com.model.bean.EmployeeFacadeLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class loginFilter implements Filter {

    @EJB
    private EmployeeFacadeLocal employeeFacadeLocal;
    private final String DEFAULTPAGE = "/BluePumpkinCompany/";
    private final String LOGINPAGE = "/Login.xhtml";
    private final String HOMEPAGE = "/Home.xhtml";
    private final String HELPPAGE = "/BluePumpkinCompany/Help.xhtml";
    private final String LOGINSESSION = "login";
    private final String USERNAME = "username";
    private final String PASSWORD = "passwd";

    private HttpServletResponse response;
    private HttpServletRequest request;
    private String username;
    private String password;
    private String fullname;
    private String url;

    public loginFilter() {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        this.response = (HttpServletResponse) response;
        this.request = (HttpServletRequest) request;
        getValueCookie();
        if (!checkUser()) {
            if (checkVisitorsURL()) {
                if (!checkUrl()) {
                    redirect(LOGINPAGE);
                } else {
                    chain.doFilter(this.request, this.response);
                }
            }else{
               chain.doFilter(this.request, this.response);
            }
        } else {
            if (checkUrl()) {
                redirect(HOMEPAGE);
            } else {
                chain.doFilter(this.request, this.response);
            }
        }
    }

    public void getValueCookie() {
        String cookieName;
        Cookie cookie[] = this.request.getCookies();
        if (cookie != null && cookie.length > 0) {
            for (Cookie cookie1 : cookie) {
                cookieName = cookie1.getName();
                switch (cookieName) {
                    case USERNAME:
                        username = cookie1.getValue();
                        break;
                    case PASSWORD:
                        password = cookie1.getValue();
                        break;
                }
            }
            if (username != null && password != null) {
                checkAcc(username, password);
            }
        }
    }

    private void checkAcc(String username, String password) {
        Employee em = employeeFacadeLocal.find(username);
        if (em != null) {
            if (em.getPassword().equals(password)) {
                fullname = em.getFullname();
                saveLoginToSession();
            }
        }
    }

    private boolean checkUrl() {
        url = this.request.getRequestURI();
        if (url.equals(DEFAULTPAGE)) {
            return true;
        }
        return url.contains(LOGINPAGE);
    }

    private boolean checkVisitorsURL() {
        url = this.request.getRequestURI();
        System.out.println("==============0"+url);
        if (url.equals(HELPPAGE)) {
            return false;
        }
        return true;
    }

    private void redirect(String url) throws IOException {
        this.response.sendRedirect(this.request.getServletContext().getContextPath() + url);
    }

    private boolean checkUser() {
        HttpSession session = this.request.getSession(false);
        Login useruser = (session != null) ? (Login) session.getAttribute(LOGINSESSION) : null;
        if (useruser != null) {
            return (useruser.getFullname() != null);
        }
        return false;
    }

    private void saveLoginToSession() {
        HttpSession session = ((HttpServletRequest) this.request).getSession();
        Login user = new Login();
        user.setFullname(fullname);
        session.setAttribute(USERNAME, username);
        session.setAttribute(LOGINSESSION, user);
    }

}
